/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FootballDatabase.java
 *
 * Created on 2011-05-31, 20:47:18
 */
package football.ui;

import football.entity.Coach;
import football.entity.League;
import football.entity.Team;
import football.util.HibernateUtil;
import java.util.List;
import java.util.Vector;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;

/**
 *
 * @author Szymon
 */
public class FootballDB extends javax.swing.JFrame {

    /** Creates new form FootballDatabase */
    public FootballDB() {
        initComponents();
        fillIdLeagueComboBox();
        fillIdCoachComboBox();
    }

    private enum table {

        COACH, TEAM, LEAGUE
    };
    private static String QUERY_BASED_ON_NAME = "from Coach coach where coach.name like '";
    private static String QUERY_BASED_ON_SURNAME = "from Coach coach where coach.surname like '";
    private static String QUERY_BASED_ON_TEAM_NAME = "from Coach coach where coach.team.name like '";
    private static String QUERY_SELECT_ALL_COACHES = "from Coach";
    private static String QUERY_SELECT_ALL_LEAGUES = "from League";
    private static String QUERY_SELECT_ALL_TEAMS = "from Team";

    private void fillIdCoachComboBox() {
        List resultList = null;
        try {
            Session session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            Query q = session.createQuery(QUERY_SELECT_ALL_COACHES);
            resultList = q.list();
            session.getTransaction().commit();
        } catch (HibernateException he) {
            he.printStackTrace();
        }
        Vector<Object> idNumbers = new Vector<Object>();
        for (Object o : resultList) {
            Coach coach = (Coach) o;
            idNumbers.add(coach.getIdCoach());
        }
        idCoachComboBox.setModel(new DefaultComboBoxModel(idNumbers));
    }

    private void fillIdLeagueComboBox() {

        List resultList = null;
        try {
            Session session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            Query q = session.createQuery(QUERY_SELECT_ALL_LEAGUES);
            resultList = q.list();
            session.getTransaction().commit();
        } catch (HibernateException he) {
            he.printStackTrace();
        }
        Vector<Object> idNumbers = new Vector<Object>();
        for (Object o : resultList) {
            League league = (League) o;
            idNumbers.add(league.getIdLeague());
        }
        idLeagueComboBox.setModel(new DefaultComboBoxModel(idNumbers));
    }

    private void runQueryBasedOnName() {
        executeHQLQuery(QUERY_BASED_ON_NAME + nameTextField.getText() + "%'", table.COACH);
    }

    private void runQueryBasedOnSurname() {
        executeHQLQuery(QUERY_BASED_ON_SURNAME + surnameTextField.getText() + "%'", table.COACH);
    }

    private void runQueryBasedOnTeamName() {
        executeHQLQuery(QUERY_BASED_ON_TEAM_NAME + teamNameTextField.getText() + "%'", table.COACH);
    }

    private void runQuerySelectAllCoaches() {
        executeHQLQuery(QUERY_SELECT_ALL_COACHES, table.COACH);
    }

    private void runQuerySelectAllLeagues() {
        executeHQLQuery(QUERY_SELECT_ALL_LEAGUES, table.LEAGUE);
    }

    private void runQuerySelectAllTeams() {
        executeHQLQuery(QUERY_SELECT_ALL_TEAMS, table.TEAM);
    }

    private void executeHQLQuery(String hql, table t) {
        try {
            Session session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            Query q = session.createQuery(hql);
            List resultList = q.list();
            displayResult(resultList, t);
            session.getTransaction().commit();
        } catch (HibernateException he) {
            he.printStackTrace();
        }
    }

    private void executeHQLInsert(table t, String name, Integer goalsC, Integer goalsS, Integer idLeague) {
        try {
            Session session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();

            switch (t) {
                case TEAM:
                    Team team = new Team();
                    team.setName(name);
                    team.setGoalsConceded(goalsC);
                    team.setGoalsScored(goalsS);
                    Query q = session.createQuery("from League where id_league = " + Integer.toString(idLeague));
                    List resultList = q.list();
                    League league = (League) resultList.get(0);
                    team.setLeague(league);
                    session.save(team);
                    break;
            }
            session.getTransaction().commit();
        } catch (HibernateException he) {
            he.printStackTrace();
        }
    }
    
    private void executeHQLUpdate(table t, String name, Integer goalsC, Integer goalsS, Integer idTeam) {
        try {
            Session session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();

            switch (t) {
                case TEAM:
                    Query q = session.createQuery("from Team team where id_team = " + Integer.toString(idTeam));
                    Team team = (Team)q.list().get(0);
                    team.setName(name);
                    team.setGoalsConceded(goalsC);
                    team.setGoalsScored(goalsS);
                    session.update(team);
                    break;
            }
            session.getTransaction().commit();
            session.close();
        } catch (HibernateException he) {
            he.printStackTrace();
        }
    }

    private void executeHQLDelete(table t, Integer idCoach) {
        try {
            Session session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();

            switch (t) {
                case COACH:
                    Query q = session.createQuery("from Coach coach where id_coach = " + Integer.toString(idCoach));
                    Coach coach = (Coach)q.list().get(0);
                    session.delete(coach);
                    break;
            }
            session.getTransaction().commit();
            session.close();
        } catch (HibernateException he) {
            he.printStackTrace();
        }
        fillIdCoachComboBox();
    }

    private void displayResult(List resultList, table t) {
        Vector<String> tableHeaders = new Vector<String>();
        Vector tableData = new Vector();

        switch (t) {
            case COACH:
                tableHeaders.add("CoachId");
                tableHeaders.add("Name");
                tableHeaders.add("Surname");
                tableHeaders.add("Team Name");

                for (Object o : resultList) {
                    Coach coach = (Coach) o;
                    Vector<Object> oneRow = new Vector<Object>();
                    oneRow.add(coach.getIdCoach());
                    oneRow.add(coach.getName());
                    oneRow.add(coach.getSurname());
                    oneRow.add(coach.getTeam().getName());
                    tableData.add(oneRow);
                }
                resultTable.setModel(new DefaultTableModel(tableData, tableHeaders));
                break;

            case LEAGUE:
                tableHeaders.add("LeagueId");
                tableHeaders.add("Name");
                tableHeaders.add("Level");
                tableHeaders.add("Number of teams");

                for (Object o : resultList) {
                    League league = (League) o;
                    Vector<Object> oneRow = new Vector<Object>();
                    oneRow.add(league.getIdLeague());
                    oneRow.add(league.getName());
                    oneRow.add(league.getLevel());
                    oneRow.add(league.getTeams().size());
                    tableData.add(oneRow);
                }
                resultTable.setModel(new DefaultTableModel(tableData, tableHeaders));
                break;

            case TEAM:
                tableHeaders.add("TeamId");
                tableHeaders.add("Name");
                tableHeaders.add("Goals Conceded");
                tableHeaders.add("Goals Scored");
                tableHeaders.add("League name");
                tableHeaders.add("League level");

                for (Object o : resultList) {
                    Team team = (Team) o;
                    Vector<Object> oneRow = new Vector<Object>();
                    oneRow.add(team.getIdTeam());
                    oneRow.add(team.getName());
                    oneRow.add(team.getGoalsConceded());
                    oneRow.add(team.getGoalsScored());
                    oneRow.add(team.getLeague().getName());
                    oneRow.add(team.getLeague().getLevel());
                    tableData.add(oneRow);
                }
                resultTable.setModel(new DefaultTableModel(tableData, tableHeaders));
                break;
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        footballPUEntityManager = java.beans.Beans.isDesignTime() ? null : javax.persistence.Persistence.createEntityManagerFactory("footballPU").createEntityManager();
        leagueQuery = java.beans.Beans.isDesignTime() ? null : footballPUEntityManager.createQuery("SELECT l FROM League l");
        leagueList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : leagueQuery.getResultList();
        jLabel1 = new javax.swing.JLabel();
        nameLabel = new javax.swing.JLabel();
        nameTextField = new javax.swing.JTextField();
        surnameLabel = new javax.swing.JLabel();
        surnameTextField = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        resultTable = new javax.swing.JTable();
        searchNameButton = new javax.swing.JButton();
        searchSurnameButton = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        teamNameTextField = new javax.swing.JTextField();
        teamNameSearchButton = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        idTeamLabel = new javax.swing.JLabel();
        idTeamTextField = new javax.swing.JTextField();
        teamNameLabel = new javax.swing.JLabel();
        teamNameTextField2 = new javax.swing.JTextField();
        goalsConcededLabel = new javax.swing.JLabel();
        goalsConcededTextField = new javax.swing.JTextField();
        goalsScoredLabel = new javax.swing.JLabel();
        goalsScoredTextField = new javax.swing.JTextField();
        insertTeamButton = new javax.swing.JButton();
        updateTeamButton = new javax.swing.JButton();
        jLabel8 = new javax.swing.JLabel();
        idCoachComboBox = new javax.swing.JComboBox();
        deleteCoachButton = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        selectCoachButton = new javax.swing.JButton();
        selectTeamButton = new javax.swing.JButton();
        selectLeagueButton = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        idLeagueComboBox = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("Search data (coach)");

        nameLabel.setText("Name:");

        surnameLabel.setText("Surname:");

        resultTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        jScrollPane1.setViewportView(resultTable);

        searchNameButton.setText("Search");
        searchNameButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                searchNameButtonActionPerformed(evt);
            }
        });

        searchSurnameButton.setText("Search");
        searchSurnameButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                searchSurnameButtonActionPerformed(evt);
            }
        });

        jLabel2.setText("Team name:");

        teamNameSearchButton.setText("Search");
        teamNameSearchButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                teamNameSearchButtonActionPerformed(evt);
            }
        });

        jLabel3.setText("Insert/Update (team)");

        idTeamLabel.setText("id_team: ");

        teamNameLabel.setText("Name:");

        goalsConcededLabel.setText("Goals conceded:");

        goalsScoredLabel.setText("Goals scored:");

        insertTeamButton.setText("Insert");
        insertTeamButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                insertTeamButtonActionPerformed(evt);
            }
        });

        updateTeamButton.setText("Update");
        updateTeamButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                updateTeamButtonActionPerformed(evt);
            }
        });

        jLabel8.setText("Delete data (coach)");

        idCoachComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        deleteCoachButton.setText("Delete");
        deleteCoachButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteCoachButtonActionPerformed(evt);
            }
        });

        jLabel4.setText("Show tables");

        selectCoachButton.setText("Coaches");
        selectCoachButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                selectCoachButtonActionPerformed(evt);
            }
        });

        selectTeamButton.setText("Teams");
        selectTeamButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                selectTeamButtonActionPerformed(evt);
            }
        });

        selectLeagueButton.setText("Leagues");
        selectLeagueButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                selectLeagueButtonActionPerformed(evt);
            }
        });

        jLabel5.setText("id_league:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(nameLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(nameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(searchNameButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(surnameLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(surnameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(searchSurnameButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(teamNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(teamNameSearchButton))
                    .addComponent(jLabel3)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 743, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(idTeamLabel)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(idTeamTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(teamNameLabel))
                                    .addComponent(jLabel8))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(teamNameTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(selectCoachButton)
                                        .addComponent(jLabel4))))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(idCoachComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(deleteCoachButton)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(goalsConcededLabel)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(goalsConcededTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(goalsScoredLabel)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(goalsScoredTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jLabel5)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(idLeagueComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(19, 19, 19)
                                .addComponent(insertTeamButton)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(updateTeamButton))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(selectTeamButton)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(selectLeagueButton)))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(nameLabel)
                    .addComponent(nameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(searchNameButton)
                    .addComponent(surnameLabel)
                    .addComponent(surnameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(searchSurnameButton)
                    .addComponent(jLabel2)
                    .addComponent(teamNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(teamNameSearchButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel3)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(idTeamLabel)
                    .addComponent(idTeamTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(teamNameLabel)
                    .addComponent(teamNameTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(goalsConcededLabel)
                    .addComponent(goalsConcededTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(goalsScoredLabel)
                    .addComponent(goalsScoredTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5)
                    .addComponent(updateTeamButton)
                    .addComponent(insertTeamButton)
                    .addComponent(idLeagueComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(idCoachComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(deleteCoachButton)
                    .addComponent(selectCoachButton)
                    .addComponent(selectLeagueButton)
                    .addComponent(selectTeamButton))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 147, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jLabel1.getAccessibleContext().setAccessibleName("JLabel1");

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void searchNameButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_searchNameButtonActionPerformed
        if (!nameTextField.getText().trim().equals("")) {
            runQueryBasedOnName();
        }
    }//GEN-LAST:event_searchNameButtonActionPerformed

    private void searchSurnameButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_searchSurnameButtonActionPerformed
        if (!surnameTextField.getText().trim().equals("")) {
            runQueryBasedOnSurname();
        }
    }//GEN-LAST:event_searchSurnameButtonActionPerformed

    private void teamNameSearchButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_teamNameSearchButtonActionPerformed
        if (!teamNameTextField.getText().trim().equals("")) {
            runQueryBasedOnTeamName();
        }
    }//GEN-LAST:event_teamNameSearchButtonActionPerformed

    private void selectCoachButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_selectCoachButtonActionPerformed
        runQuerySelectAllCoaches();
    }//GEN-LAST:event_selectCoachButtonActionPerformed

    private void selectLeagueButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_selectLeagueButtonActionPerformed
        runQuerySelectAllLeagues();
    }//GEN-LAST:event_selectLeagueButtonActionPerformed

    private void selectTeamButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_selectTeamButtonActionPerformed
        runQuerySelectAllTeams();
    }//GEN-LAST:event_selectTeamButtonActionPerformed

    private void insertTeamButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_insertTeamButtonActionPerformed
        if (!teamNameTextField2.getText().trim().equals("") && !goalsConcededTextField.getText().trim().equals("") && !goalsScoredTextField.getText().trim().equals("")) {

            Integer goalsC = null;
            Integer goalsS = null;
            Integer idLeague = null;

            try {
                goalsC = Integer.parseInt(goalsConcededTextField.getText());
                goalsS = Integer.parseInt(goalsScoredTextField.getText());
                idLeague = (Integer) idLeagueComboBox.getSelectedItem();
            } catch (Exception e) {
                JOptionPane.showMessageDialog(null,
                        "Goals' fields should be numeric and integer",
                        "Error",
                        JOptionPane.ERROR_MESSAGE);
                return;
            }
            executeHQLInsert(table.TEAM, teamNameTextField2.getText(), goalsC, goalsS, idLeague);
        }
    }//GEN-LAST:event_insertTeamButtonActionPerformed

    private void deleteCoachButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteCoachButtonActionPerformed
        executeHQLDelete(table.COACH, (Integer) idCoachComboBox.getSelectedItem());
        System.out.println("Weszło");
    }//GEN-LAST:event_deleteCoachButtonActionPerformed

    private void updateTeamButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_updateTeamButtonActionPerformed
        if (!idTeamTextField.getText().trim().equals("") && !teamNameTextField2.getText().trim().equals("") && !goalsConcededTextField.getText().trim().equals("") && !goalsScoredTextField.getText().trim().equals("")) {

            Integer goalsC = null;
            Integer goalsS = null;
            Integer idTeam = null;

            try {
                goalsC = Integer.parseInt(goalsConcededTextField.getText());
                goalsS = Integer.parseInt(goalsScoredTextField.getText());
                idTeam = Integer.parseInt(idTeamTextField.getText());
            } catch (Exception e) {
                JOptionPane.showMessageDialog(null,
                        "Goals' fields should be numeric and integer",
                        "Error",
                        JOptionPane.ERROR_MESSAGE);
                return;
            }
            executeHQLUpdate(table.TEAM, teamNameTextField2.getText(), goalsC, goalsS, idTeam);
        }
    }//GEN-LAST:event_updateTeamButtonActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new FootballDB().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton deleteCoachButton;
    private javax.persistence.EntityManager footballPUEntityManager;
    private javax.swing.JLabel goalsConcededLabel;
    private javax.swing.JTextField goalsConcededTextField;
    private javax.swing.JLabel goalsScoredLabel;
    private javax.swing.JTextField goalsScoredTextField;
    private javax.swing.JComboBox idCoachComboBox;
    private javax.swing.JComboBox idLeagueComboBox;
    private javax.swing.JLabel idTeamLabel;
    private javax.swing.JTextField idTeamTextField;
    private javax.swing.JButton insertTeamButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JScrollPane jScrollPane1;
    private java.util.List<football.ui.League> leagueList;
    private javax.persistence.Query leagueQuery;
    private javax.swing.JLabel nameLabel;
    private javax.swing.JTextField nameTextField;
    private javax.swing.JTable resultTable;
    private javax.swing.JButton searchNameButton;
    private javax.swing.JButton searchSurnameButton;
    private javax.swing.JButton selectCoachButton;
    private javax.swing.JButton selectLeagueButton;
    private javax.swing.JButton selectTeamButton;
    private javax.swing.JLabel surnameLabel;
    private javax.swing.JTextField surnameTextField;
    private javax.swing.JLabel teamNameLabel;
    private javax.swing.JButton teamNameSearchButton;
    private javax.swing.JTextField teamNameTextField;
    private javax.swing.JTextField teamNameTextField2;
    private javax.swing.JButton updateTeamButton;
    // End of variables declaration//GEN-END:variables
}
